package com.nti56.springboot.utils;



import java.math.BigDecimal;

public class NumberUtil 
{
	public static boolean isNumber(String numStr)
	{
		try
		{
			if (numStr == null || numStr.trim().equals(""))
				return false;
			new BigDecimal(numStr);
			return true;
		}
		catch (NumberFormatException e) 
		{ 
			return false; 
		}
	}
	
	public static Integer toInteger(String str)
	{
		Integer integer = null;
		if(str != null && str.trim().length() > 0)
		{
			try
			{
				integer = new Integer(Integer.parseInt(str));
			}
			catch (Exception e) { e.printStackTrace(); }
		}
		return integer;
	}
	
	public static Integer toInteger(Object object)
	{
		Integer integer = null;
		if(object!=null)
		{
			if (object instanceof BigDecimal)
			{
				return ((BigDecimal) object).intValue();
			}
			
			String string = object.toString();			
			integer = toInteger(string);
		}
		return integer;
	}
	
	
	public static BigDecimal toBigDecimal(String value)
	{
		if(isNumber(value))
		{
			return new BigDecimal(value);
		}
		return null;
		
	}
	
	public static Double toDouble(String aString) 
	{
		try
		{
			double value = 0.00;
			if (aString == null || aString.trim().equals(""))
				return 0.00;
			value = Double.parseDouble(aString.trim());
			return value;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
}
